package com.auth.manager.security.service;

import com.auth.manager.pojo.sys.UserInfo;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @version 1.0.0
 * @Date: 2022/4/26 20:36
 * @Author ZhuYouBin
 * @Description 用户登录账号信息实体类
 */
public class UserAccount implements UserDetails {
    // 用户信息相关字段
    private String username;
    private String password;
    private boolean isAccountNonExpired; // 账号未过期
    private boolean isAccountNonLocked; // 账号未锁定
    private boolean isCredentialsNonExpired; // 凭证未过期
    private boolean isEnabled; // 账号是否可用
    private List<String> roleLists; // 用户角色信息
    private UserInfo userInfo; // 用户信息

    public UserAccount(String username, String password, List<String> roleLists, UserInfo userInfo) {
        this(username, password, roleLists, userInfo, true, true, true, true);
    }

    public UserAccount(String username, String password, List<String> roleLists, UserInfo userInfo, boolean isAccountNonExpired, boolean isAccountNonLocked, boolean isCredentialsNonExpired, boolean isEnabled) {
        this.username = username;
        this.password = password;
        this.roleLists = roleLists;
        this.isAccountNonExpired = isAccountNonExpired;
        this.isAccountNonLocked = isAccountNonLocked;
        this.isCredentialsNonExpired = isCredentialsNonExpired;
        this.isEnabled = isEnabled;
        this.userInfo = userInfo;
    }

    // 用户权限集合
    // security会调用这个方法获取角色信息,然后判断当前登录用户是否具有访问资源的权限
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> list = new ArrayList<>();
        if (roleLists != null) {
            for (String role : roleLists) {
                SimpleGrantedAuthority authority = new SimpleGrantedAuthority(role);
                list.add(authority);
            }
        }
        return list;
    }
    @Override
    public String getUsername() {
        return this.username;
    }
    @Override
    public String getPassword() {
        return this.password;
    }
    @Override
    public boolean isAccountNonExpired() {
        return this.isAccountNonExpired;
    }
    @Override
    public boolean isAccountNonLocked() {
        return this.isAccountNonLocked;
    }
    @Override
    public boolean isCredentialsNonExpired() {
        return this.isCredentialsNonExpired;
    }
    @Override
    public boolean isEnabled() {
        return this.isEnabled;
    }
    public UserInfo getUserInfo() {
        return this.userInfo;
    }
}
